package com.shiguiwu.springmybatis.javabase.reflect;

import cn.hutool.core.util.ReflectUtil;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;

/**
 * @description: ceshi
 * @author: stone
 * @date: Created by 2021/2/7 17:32
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.reflect
 */
public class ReflectTests {


    public static void main(String[] args) throws Exception {
//        Class<Person> personClass = Person.class;
        Class<Person> personClass = (Class<Person>) Class.forName("com.shiguiwu.springmybatis.javabase.reflect.Person");

        Person person = personClass.newInstance();
        Field name = personClass.getDeclaredField("name");
        name.setAccessible(true);
        AName annotation = name.getAnnotation(AName.class);
        name.set(person, annotation.name());

        Method setAge = personClass.getDeclaredMethod("setAge", Integer.class);
        setAge.invoke(person, 18);
        Method[] declaredMethods = personClass.getDeclaredMethods();
//        System.out.println(Arrays.toString(declaredMethods));

//        System.out.println(person.toString());

        Constructor<Person> declaredConstructor = personClass.getDeclaredConstructor(String.class, Integer.class);

        Person dog = declaredConstructor.newInstance("小狗", 33);
//        System.out.println(dog);

        Constructor<Person>[] constructors = ReflectUtil.getConstructors(Person.class);

        for (int i = 0; i < constructors.length; i++) {
            System.out.println(Arrays.toString(constructors[i].getParameters()));

            Parameter[] parameters = constructors[i].getParameters();
            Class<?> type = parameters[1].getType();
            System.out.println(type.getName());

        }

        Field[] fields = ReflectUtil.getFields(Person.class);

        Method[] methods = ReflectUtil.getMethods(Person.class);

//        ReflectUtil.getFieldsDirectly()

    }}
